package com.caigou.modules.vote.viewmodel

import android.text.TextUtils
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.caigou.comm.base.bean.CommentBean
import com.caigou.comm.base.ext.Resource
import com.caigou.comm.base.utils.Logger
import com.caigou.modules.vote.bean.UnlikeBean
import com.caigou.modules.vote.repository.VoteRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import javax.inject.Inject

/**
 * @author #Suyghur.
 * Created on 2021/12/30
 */
@HiltViewModel
class UnlikeViewModel @Inject constructor(private val repository: VoteRepository) : ViewModel() {

    val periodListLiveData = MutableLiveData<Resource<MutableList<String>>>()
    val unlikeDetailLiveData = MutableLiveData<Resource<UnlikeBean>>()
    val commentListLiveData = MutableLiveData<Resource<CommentBean>>()

    fun getPeriodList(year: Int) {
        periodListLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getPeriodList(year)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<MutableList<String>>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    periodListLiveData.value = it
                }
        }
    }

    fun getUnlikeDetail(year: Int, period: Int, userId: String) {
        unlikeDetailLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getUnlikeDetail(year, period, userId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<UnlikeBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    unlikeDetailLiveData.value = it
                }
        }
    }

    fun getCommentList(page: Int, limit: Int, mysteryId: Int, sort: Int, userId: String) {
        commentListLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getCommentList(page, limit, mysteryId, 3, sort, userId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<CommentBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    commentListLiveData.value = it
                }
        }
    }
}